<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>仁爱宠物医院</title>
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  <script src="https://unpkg.com/vue@2/dist/vue.js"></script>
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
  <link href="https://cdn.staticfile.net/twitter-bootstrap/5.1.1/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.staticfile.net/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js"></script>
  <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<style>
  a {
    text-decoration: none;
  }

  .home_div {
    width: 60%;
    height: 85%;
    margin: auto;
    margin-top: 3%;
    padding: 2% 5%;
    position: relative;
    overflow: hidden;
    border-radius: 20px;
    box-shadow: 2px 2px 15px rgba(0, 0, 0, .2);
  }

  #userinfo {
    height: 100vh;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

<body>
  <div id="userinfo">
    <el-menu class="el-menu-demo" mode="horizontal" background-color="#545c64" text-color="#fff"
      active-text-color="#ffd04b">
      <el-menu-item><span style="font-size: 20px; font-weight: 700;">宠物仁爱医院管理端</span></el-menu-item>
      <el-menu-item><a href="./home.html">首页</a></el-menu-item>
      <el-menu-item><a href="./user.html">会员管理</a></el-menu-item>
      <el-menu-item><a href="./goods.html">商品管理</a></el-menu-item>
      <el-menu-item><a href="./order.html">订单管理</a></el-menu-item>
      <el-menu-item><a href="./userinfo.html">个人信息</a></el-menu-item>
      <el-menu-item><a href="./message.html">留言管理</a></el-menu-item>
      <el-menu-item><a href="./contact.html">联系我们</a></el-menu-item>
      <el-menu-item><a href="./login.html">退出登录</a></el-menu-item>
    </el-menu>
    <div class="home_div">
      <h3>个人信息</h3>
      <el-form :model="ruleForm" status-icon label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="name">
          <el-input type="text" v-model="ruleForm.name" autocomplete="off" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="头像" prop="img">
          <el-upload class="avatar-uploader" :action="url+'/upload'" :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="imageUrl" :src="url+'/download/'+imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input type="text" v-model="ruleForm.account" disabled style="width: 500px;"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="text" v-model="ruleForm.password" style="width: 500px;" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input type="text" v-model="ruleForm.email" style="width: 500px;"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogVisible = true">修改密码</el-button>
          <el-button type="primary" @click="submitForm()">提交</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 修改密码 -->
    <el-dialog title="修改密码" :visible.sync="dialogVisible" width="30%">
      <el-form :model="dia_form" status-icon label-width="100px" class="demo-ruleForm">
        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model="dia_form.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="checkPass">
          <el-input type="password" v-model="dia_form.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="password">
          <el-input type="password" v-model="dia_form.password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updatePass()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</body>
<script>
  new Vue({
    el: '#userinfo',
    data() {
      return {
        dialogVisible: false,
        imageUrl: '',
        ruleForm: {},
        dia_form: {},
        url: 'http://localhost:8082/common'
      };
    },
    created() {
      // 要先确保用户已经登录
      if (localStorage.getItem("userId") == null) {
        // 表示没有登录，跳转到登录页面
        window.location.href = './login.html';
        return;
      }
      this.page_init();
    },
    methods: {
      // 确定修改密码
      async updatePass() {
        if (this.dia_form.checkPass !== this.dia_form.password) {
          this.$message.error("请确保两次输入密码一样");
        }
        this.ruleForm.id = localStorage.getItem("userId");
        this.ruleForm.name = this.dia_form.pass;
        this.ruleForm.password = this.dia_form.password;
        const { data: result } = await this.$http.post('http://localhost:8082/user_backend/updatePassword', this.ruleForm);
        if (result.flag) {
          // 修改成功
          this.$message.success(result.message);
          localStorage.removeItem("userId");
          window.location.href = './login.html';
        } else {
          this.$message.error(result.message);
        }
        this.dialogVisible = false;
      },
      // 修改用户
      async submitForm() {
        this.ruleForm.id = localStorage.getItem("userId");
        this.ruleForm.img = this.imageUrl;
        const { data: result } = await this.$http.post('http://localhost:8082/user_backend/updateInfo', this.ruleForm);
        if (result.flag) {
          this.$message.success(result.message);
        } else {
          this.$message.error(result.message);
        }
        this.page_init();
      },
      // 上传图片成功时
      handleAvatarSuccess(res, file) {
        this.imageUrl = res.data;
      },
      // 刷新页面
      async page_init() {
        const { data: result } = await this.$http.get('http://localhost:8082/user_backend/queryUser/' + localStorage.getItem("userId"));
        this.ruleForm = result.l;
        // 把图片赋值给相对应的路径
        this.imageUrl = this.ruleForm.img;
      }
    }
  })
</script>

</html>